//
//  ViewController.m
//  17-UI-NSFileManager
//
//  Created by Snow on 16/8/5.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *
     实现文件、文件夹的基本操作(调研NSFileManager, 并实现以下功能)
     创建文件、文件夹
     删除文件、文件夹
     文件 是否存在
     移动文件
     拷贝文件
     *
     */
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSFileManager *file = [NSFileManager defaultManager];
    //创建文件
    NSString *pathOfFile = [[pathArray firstObject] stringByAppendingString:@"Look"];
    NSString *string = @"创建文件";
    BOOL isFile = [file createFileAtPath:pathOfFile contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    if (isFile) {
        NSLog(@"创建文件成功");
    } else {
        NSLog(@"创建文件失败");
    }
    //创建文件夹
    NSString *pathOfFiles = [[pathArray firstObject] stringByAppendingString:@"test"];
    BOOL isTest = [file createDirectoryAtPath:pathOfFiles withIntermediateDirectories:YES attributes:nil error:nil];
    if (isTest) {
        NSLog(@"创建文件夹成功");
    } else {
        NSLog(@"创建文件夹失败");
    }
    //删除文件
    BOOL isRemoveFile = [file removeItemAtPath:pathOfFile error:nil];
    if (isRemoveFile) {
        NSLog(@"删除文件成功");
    } else {
        NSLog(@"删除文件失败");
    }
    //删除文件夹
    BOOL isRemoveFiles = [file removeItemAtPath:pathOfFiles error:nil];
    if (isRemoveFiles) {
        NSLog(@"删除文件夹成功");
    } else {
        NSLog(@"删除文件夹失败");
    }
    //判断文件是否存在
    if ([file fileExistsAtPath:pathOfFile]) {
        NSLog(@"存在");
    } else {
        NSLog(@"不存在");
    }
    //移动文件
    NSString *movePath = [[pathArray firstObject] stringByAppendingString:@"moveFile"];
    BOOL isMove = [file moveItemAtPath:movePath toPath:pathOfFile error:nil];
    if (!isMove) {
        NSLog(@"移动成功");
    } else {
        NSLog(@"移动失败");
    }
    //拷贝文件
    NSString *copyPath = [[pathArray firstObject] stringByAppendingString:@"copyFile"];
    BOOL isCopy = [file copyItemAtPath:copyPath toPath:pathOfFile error:nil];
    if (isCopy) {
        NSLog(@"拷贝成功");
    } else {
        NSLog(@"拷贝失败");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
